$(function(){
      
    $("#form_aluno").validate({
        onfocusout: function (element) {
            $(element).valid();
        },
        errorPlacement: function(error, element) 
        {
            element.attr('title', error.text());
            $(".error").tooltip({   
                position: {
                    my: "center bottom-10",
                    at: "right top",
                    using: function(position, feedback) {
                        $(this).css(position);
                        $("<div>").addClass("arrow").addClass(feedback.vertical).addClass(feedback.horizontal).appendTo(this);
                    }
                },
                tooltipClass: "ttError"
            }); 
        },
        rules: {
            ALU_SNH: { required: true, minlength: 6 },
            ALU_SNH2: { required: true, minlength: 6, equalTo: "#ALU_SNH" },
        },
    });
    
    $.validator.addMethod("cpf", function(value, element) { var cpf = value; cpf = cpf.replace(/[^\d]+/g,''); if(cpf == '' || cpf.length != 11){ return true; } if(cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){ return false; } add = 0; for(i=0; i < 9; i ++){ add += parseInt(cpf.charAt(i)) * (10 - i); } rev = 11 - (add % 11); if(rev == 10 || rev == 11) rev = 0; if(rev != parseInt(cpf.charAt(9))) return false; add = 0; for(i = 0; i < 10; i ++){ add += parseInt(cpf.charAt(i)) * (11 - i); } rev = 11 - (add % 11); if(rev == 10 || rev == 11){ rev = 0; } if(rev != parseInt(cpf.charAt(10))){ return false; } return true; }, "CPF inválido");

    
    $("#dialog").dialog({ 
        autoOpen: false,
        modal: true,
        width: 850,
        height: 470,
        title: "Selecione o Aluno."
        
    });
    
    $(".find_alu" ).autocomplete({
        source: URLBASE+'/aluno/find',
        minLength: 1,
        select: function( event, ui ) {
            $(".find_alu" ).val(ui.item.value);
        }
    });
    
    $(".find_alu_gl").autocomplete({
        source: URLBASE+'/aluno/find',
        minLength: 1,
        select: function( event, ui ) {
            $(".find_val").val(ui.item.id);
            $(".find_rep_gl" ).val(ui.item.value);
        }
    });
    
    $('#form-search_ajx').submit(function() {
        $.post(URLBASE+'/aluno/popup?',$('#form-search_ajx').serialize(),function(data) {
            $("#dialog").html(data);
        }, 'json');
        return false;
    });
    
    $('.page_ajx').click(function() {
        $.post(URLBASE+'/aluno/popup/pagina/'+$(this).attr('page')+'?',$('#form-search_ajx').serialize(),function(data) {
            $("#dialog").html(data);
         }, 'json');
        return false;
    });
    
    $('#ALU_EML,#ALU_PAIEML,#ALU_MAEEML').blur(function() {
        el = $(this);
        $.post(URLBASE+'/aluno/validaremail/',{ id:$('#ALU_ID').val(), email:$(this).val() },function(data) {
            if(data.ok){ alert(data.msg); el.focus(); }
         }, 'json');
        return false;
    });
    
    $('#ALU_LOG').blur(function() {
        el = $(this);
        $.post(URLBASE+'/aluno/validarlogin/',{ id:$('#ALU_ID').val(), login:$(this).val() },function(data) {
            if(data.ok){ alert(data.msg); el.focus(); }
         }, 'json');
        return false;
    });
    
    $('#ALU_CPF').blur(function() {
        CPFdpl($('#ALU_ID').val(),$('#ALU_CPF').val(),'ALU_CPF');
    });
    
    $('#ALU_PAICPF').blur(function() {
        CPFdpl($('#ALU_ID').val(),$('#ALU_PAICPF').val(),'ALU_PAICPF');
    });
    
    $('#ALU_MAECPF').blur(function() {
        CPFdpl($('#ALU_ID').val(),$('#ALU_MAECPF').val(),'ALU_MAECPF');
    });
    
    $(".chosen-select").chosen(); 
    
    //$('.edit_profissao').live('click',function() { 
    $(document).on("click", ".edit_profissao", function() { 
        el = $(this);
        el.prev().prev().attr('ativo','ativo');
        modal();
        $.get(URLBASE+'/profissao/edit/',{ ajax:'ajax' },function(data) {
            $("#dialog").html(data);
            $("#dialog").dialog({ width:420,height:200,title:'Profissional'}); 
            $("#dialog").dialog("open"); 
            remove_modal();
         });
        return false;
    });
    
    $(".p_cep").blur(function(){
         if($(this).val().length > 0){
            $(this).after('<span class="ajimg"></span>');
            $.post(URLBASE+'/funcoes/ajxcep/',{ cep: $(this).val() }, function(data){
                //if(data.list['logradouro'] > ''){
                    $(".p_res_endereco").val(data.list['logradouro']+', ');
                    $(".p_res_bairro").val(data.list['bairro']);
                    $(".p_res_cidade").val(data.list['cidade']);
                    $(".p_res_uf").val(data.list['uf']);
                    $(".vres_endereco").focus();     
                //}
                $(".ajimg").remove();
                
            }, 'json');
        }
    });
    
    $(".m_cep").blur(function(){
         if($(this).val().length > 0){
            $(this).after('<span class="ajimg"></span>');
            $.post(URLBASE+'/funcoes/ajxcep/',{ cep: $(this).val() }, function(data){
                //if(data.list['logradouro'] > ''){
                    $(".m_res_endereco").val(data.list['logradouro']+', ');
                    $(".m_res_bairro").val(data.list['bairro']);
                    $(".m_res_cidade").val(data.list['cidade']);
                    $(".m_res_uf").val(data.list['uf']);
                    $(".vres_endereco").focus();     
                //}
                $(".ajimg").remove();
                
            }, 'json');
        }
    });
    
    
    
});
function setValor(val,nom){
    $('.ajx_vlr_id').val(val);
    $('.ajx_vlr_nom').val(nom);
    $("#dialog").dialog( "close" );
}

function CPFdpl(id,cpf,el){
    $.post(URLBASE+'/aluno/cpfdpl/',{ id:id, cpf:cpf },function(data) {
        if(data.ok){ alert(data.msg); $('#'+el).focus(); }
        else if(!vercpf(cpf)){ alert('CPF inválido'); $('#'+el).focus(); }
     }, 'json');
    return false;
}

function comboprofissao(id){
    var prf_id = id;
    var prf_id2 = id;
    
    if($('#ALU_PAIPRF').attr('ativo') != 'ativo'){
        prf_id = $('#ALU_PAIPRF').val();
    }
    atuprfpai(prf_id);
    $('#ALU_PAIPRF').removeAttr('ativo');
    
    if($('#ALU_MAEPRF').attr('ativo') != 'ativo'){
        prf_id2 = $('#ALU_MAEPRF').val();
    }
    atuprfmae(prf_id2);
    $('#ALU_MAEPRF').removeAttr('ativo');

    return false;
}

function atuprfpai(id){
    $.post(URLBASE+'/profissao/comboprofissao/',{ id:id, name:'ALU_PAIPRF' },function(data) {
        if(data.ok){ $('#div_ALU_PAIPRF').html(data.html); }
    }, 'json');
}
function atuprfmae(id){
    $.post(URLBASE+'/profissao/comboprofissao/',{ id:id, name:'ALU_MAEPRF' },function(data) {
       if(data.ok){ $('#div_ALU_MAEPRF').html(data.html); }
    }, 'json');
}
function vercpf(cpf){ 
    
    cpf = cpf.replace(/[^\d]+/g,'');
    
    if(cpf == '' || cpf.length != 11){ return true; } 
    
    if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
        return false;
    
    add = 0;
    
    for (i=0; i < 9; i ++) add += parseInt(cpf.charAt(i)) * (10 - i);
    rev = 11 - (add % 11);
    
    if (rev == 10 || rev == 11) rev = 0;

    if (rev != parseInt(cpf.charAt(9))) return false;
    
    add = 0;
    
    for (i = 0; i < 10; i ++) add += parseInt(cpf.charAt(i)) * (11 - i);

    rev = 11 - (add % 11);
    
    if (rev == 10 || rev == 11) rev = 0;
    
    if (rev != parseInt(cpf.charAt(10))) return false;
    
    return true;
}